package com.yang.minioBackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.minioBackend.entity.UserImage;
import com.yang.minioBackend.mapper.UserImageMapper;
import com.yang.minioBackend.service.UserImageService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author kongyue
 * @description 针对表【user_image】的数据库操作Service实现
 * @createDate 2024-06-04 13:32:06
 */
@Service
public class UserImageServiceImpl extends ServiceImpl<UserImageMapper, UserImage>
        implements UserImageService {
    @Resource
    private UserImageMapper userImageMapper;

    /**
     *
     * @param bucket 桶名称
     * @param object 文件名称
     */
    @Override
    public boolean saveOrUpdateUserImage(Long uid, String bucket, String object) {
        //操作结果标志
        boolean flag;
        //构造记录对象
        UserImage userImage = new UserImage();
        userImage.setBucket(bucket);
        userImage.setObject(object);
        userImage.setUid(uid);

        //根据用户主键查询记录
        LambdaQueryWrapper<UserImage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserImage::getUid, uid);
        Long count = userImageMapper.selectCount(wrapper);

        //不存在文件 新增模式 否则为更新模式
        if (count <= 0) {
            userImage.setCreateTime(new Date());
            flag = userImageMapper.insert(userImage) >= 1;
        } else {
            userImage.setUpdateTime(new Date());
            flag = userImageMapper.update(userImage, wrapper) >= 1;
        }
        return flag;
    }
}




